﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using KMFrame.Permission.Util;
using System.Text;

namespace lgweb.common
{
    public class BasePage : System.Web.UI.Page
    {
        private UserSession _sessionInfo = null;
        /// <summary>
        /// 用户的请求信息
        /// </summary>
        public UserSession SessionInfo
        {
            get { return this._sessionInfo; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.InitRequestInfo();
        }

        /// <summary>
        /// 初始化访问者日志信息
        /// </summary>
        /// <param name="httpRequest"></param>
        /// <param name="httpSession"></param>
        private void InitRequestInfo()
        {
            if (SessionBox.CheckUserSession())
                this._sessionInfo = SessionBox.GetUserSession();
            else
                this._sessionInfo = new UserSession(0,"",Guid.Empty);
        }
        #region 工具函数

        /// <summary>
        /// 获取session信息
        /// </summary>
        private string GetSessionInfo()
        {
            StringBuilder sSession = new StringBuilder();

            foreach (string sessionName in this.Session.Keys)
            {
                if (this.Session[sessionName] != null)
                {
                    sSession.Append(sessionName);
                    sSession.Append("=");
                    sSession.Append(this.DecodeStr(this.Session[sessionName].ToString()));
                    sSession.Append(";");
                }
                if (sSession.Length > 8000)
                {
                    break;
                }
            }

            return sSession.ToString();
        }

        //获取Session
        public string GetSession(string keyName)
        {
            return this.AdjustNullValue(this.Session[keyName]);
        }

        //获取Request,用于表单、Cookie、服务端变量
        public string GetRequest(string keyName)
        {
            if (this.GetForm(keyName) != "")
            {
                return this.GetForm(keyName);
            }
            if (this.GetQueryString(keyName) != "")
            {
                return this.GetQueryString(keyName);
            }
            if (this.GetCookie(keyName) != "")
            {
                return this.GetCookie(keyName);
            }
            if (this.GetServerVariables(keyName) != "")
            {
                return this.GetServerVariables(keyName);
            }
            return "";
        }

        /// <summary>
        /// 获取cookies信息
        /// </summary>
        private string GetCookiesInfo()
        {
            StringBuilder sCookie = new StringBuilder();
            foreach (string keyName in this.Request.Cookies.AllKeys)
            {
                if (this.Request.Cookies[keyName] != null)
                {
                    sCookie.Append(keyName);
                    sCookie.Append("=");
                    sCookie.Append(this.DecodeStr(this.Request.Cookies[keyName].Value));
                    sCookie.Append(";");
                }
                if (sCookie.Length > 8000)
                {
                    break;
                }
            }

            return sCookie.ToString();
        }

        public string GetCookie(string keyName)
        {
            if (this.Request.Cookies[keyName] == null)
                return null;
            return this.AdjustNullValue(this.Request.Cookies[keyName].Value);
        }

        /// <summary>
        /// 获取表单信息
        /// </summary>
        private string GetFormInfo()
        {
            StringBuilder sForm = new StringBuilder();
            foreach (string formName in this.Request.Form.AllKeys)
            {
                if (this.Request.Form[formName] != null)
                {
                    sForm.Append(formName);
                    sForm.Append("=");
                    sForm.Append(this.DecodeStr(this.Request.Form[formName]));
                    sForm.Append(";");
                }
                if (sForm.Length > 8000)
                {
                    break;
                }
            }

            return sForm.ToString();
        }

        public string GetForm(string keyName)
        {
            return this.AdjustNullValue(this.Request.Form[keyName]);
        }

        public string GetQueryString(string keyName)
        {
            return this.AdjustNullValue(UnEscape(this.Request.QueryString[keyName])); ;
        }

        public string GetServerVariables(string keyName)
        {
            return this.AdjustNullValue(this.Request.ServerVariables[keyName]); ;
        }

        public void SetSession(string keyName, string value)
        {
            this.Session[keyName] = value;
        }

        public void SetCookie(string keyName, string value)
        {
            if (this.Response.Cookies[keyName] != null)
            {
                this.Response.Cookies[keyName].Value = value;
            }
        }

        public String UnEscape(string s)
        {
            if (!string.IsNullOrEmpty(s))
            {
                //+号比较特殊
                s = s.Replace("+", "%2b");

                return System.Web.HttpUtility.UrlDecode(s);
            }
            else
            {
                return s;
            }
        }
        public String Escape(string s)
        {
            return System.Web.HttpUtility.UrlEncode(s);

        }

        /// <summary>
        /// 分号转义成"%59";等号转义成"%61";百分号转义成"%37"
        /// </summary>
        private string DecodeStr(string str)
        {
            string tempStr = str;
            tempStr = tempStr.Replace("%", "%37");
            tempStr = tempStr.Replace(";", "%59");
            tempStr = tempStr.Replace("=", "%61");

            return tempStr;
        }

        public string AdjustNullValue(object value)
        {
            return value == null ? "" : value.ToString();
        }

        public int AdjustIntValue(string value)
        {
            int nValue = 0;
            try
            {
                nValue = value == null ? 0 : Convert.ToInt32(value);
            }
            catch
            {
            }
            return nValue;
        }

        #endregion
    }
}